package com.chalk.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.util.Result;
import com.chalk.config.base.BaseController;
import com.chalk.model.SysUserRoleRelated;
import com.chalk.service.SysUserRoleRelatedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 平台用户角色关联信息(SysUserRoleRelated)表控制层
 *
 * @author maxiaoqiang
 * @since 2019-03-22 17:38:26
 */
@Api(value = "平台用户角色关联信息接口", tags = "平台用户角色关联信息模块")
@RestController
@RequestMapping("/v1/sysUserRoleRelated")
public class SysUserRoleRelatedController extends BaseController {

    @Autowired
    private SysUserRoleRelatedService sysUserRoleRelatedService;

    /**
     * 平台用户角色关联信息列表
     *
     * @param current            当前页数
     * @param size               每页显示条数
     * @param sysUserRoleRelated 查询实体
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询平台用户角色关联信息列表", notes = "查询平台用户角色关联信息列表", response = Result.class)
    @GetMapping
    public Result selectAll(Integer current, Integer size, SysUserRoleRelated sysUserRoleRelated) {
        return Result.success(this.sysUserRoleRelatedService.selectPage(this.getPage(current, size), new EntityWrapper<SysUserRoleRelated>(sysUserRoleRelated)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询平台用户角色关联信息", notes = "查询平台用户角色关联信息", response = Result.class)
    @GetMapping("{id}")
    public Result selectOne(
            @ApiParam(name = "id", value = "主键", required = true, example = "1")
            @PathVariable String id) {
        return Result.success(this.sysUserRoleRelatedService.selectById(id));
    }


    /**
     * 保存平台用户角色关联信息
     *
     * @param sysUserRoleRelated 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "保存sysUserRoleRelated", notes = "保存sysUserRoleRelated", response = Result.class)
    @PostMapping
    public Result insert(
            @ApiParam(name = "sysUserRoleRelated", value = "传入json格式", required = true)
            @RequestBody SysUserRoleRelated sysUserRoleRelated) {
        return Result.success(this.sysUserRoleRelatedService.insert(sysUserRoleRelated));
    }

    /**
     * 修改平台用户角色关联信息
     *
     * @param sysUserRoleRelated 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "修改平台用户角色关联信息", notes = "修改平台用户角色关联信息", response = Result.class)
    @PutMapping
    public Result update(
            @ApiParam(name = "sysUserRoleRelated", value = "传入json格式", required = true)
            @RequestBody SysUserRoleRelated sysUserRoleRelated) {
        return Result.success(this.sysUserRoleRelatedService.updateById(sysUserRoleRelated));
    }

    /**
     * 逻辑删除平台用户角色关联信息
     *
     * @param ids 主键集合
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "批量删除平台用户角色关联信息", notes = "批量删除平台用户角色关联信息", response = Result.class)
    @DeleteMapping
    public Result delete(
            @ApiParam(name = "ids", value = "传数组", required = true)
            @RequestBody String[] ids) {
        return Result.success(this.sysUserRoleRelatedService.deleteBatchByPrimaryKey(ids));
    }

    private Page<SysUserRoleRelated> getPage(Integer current, Integer size) {
        Page<SysUserRoleRelated> page = new Page<>();
        if (current != null) {
            page.setCurrent(current);
        }
        if (size != null) {
            page.setSize(size);
        }
        return page;
    }

}